package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.pojo.dto.CustomPkSetDTO;
import com.xbongbong.paas.pojo.vo.CustomPkSetVO;
import com.xbongbong.paas.redis.PaasRedisHelper;
import com.xbongbong.paas.service.CustomPkService;
import com.xbongbong.paas.service.PkSetService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @author weiming
 * @ProjectName xbb-pro
 * @Description: 自定义PK榜控制类
 * @date 2021/4/7 9：18
 */
@RestController
@RequestMapping(value = XbbProConstant.API_ROOT_PREFIX + "/custom/pk")
public class CustomPkController {

    @Resource
    private PkSetService pkSetService;
    @Resource
    private CustomPkService customPkService;
    @Resource
    private PaasRedisHelper paasRedisHelper;

    private static final Logger LOG = LoggerFactory.getLogger(CustomPkController.class);

    /**
     * 保存自定义pk榜
     * @param customPkSetDTO
     * @param br
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/saveCustomPk", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getConfig(@RequestBody @Valid CustomPkSetDTO customPkSetDTO, BindingResult br) {
        XbbResponse<CustomPkSetVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                // ----------------------------------repeat post----------------------------------------------
                // 判断是否跳过重复提交校验
                if (!Boolean.TRUE.equals(customPkSetDTO.getIgnoreConCheck())) {
                    //重复提交校验
                    if (!paasRedisHelper.checkConcurrentLock(customPkSetDTO)) {
                        response = new XbbResponse<>(new XbbException(SystemErrorCodeEnum.API_ERROR_100003));
                        return JSON.toJSONString(response);
                    }
                }
                // ----------------------------------repeat post----------------------------------------------
                CustomPkSetVO  customPkSetVO = customPkService.saveCustomPk(customPkSetDTO);
                response = new XbbResponse<>(customPkSetVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }


        }
        return JSON.toJSONString(response);
    }

}
